/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import java.sql.Date;
import java.sql.Time;

/**
 * Classe décrivant les messages
 *
 * @author vincent
 */
public class Message {

    private Profile sender;
    private Profile receiver;
    private String text;
    private Date dateMsg;
    private Time timeMsg;
    private boolean delivered;

    public Message(Profile sender, Profile receiver, String text, Date dateMsg, Time timeMsg, boolean delivered) {
        this.sender = sender;
        this.receiver = receiver;
        this.text = secureText(text);
        this.dateMsg = dateMsg;
        this.timeMsg = timeMsg;
        this.delivered = delivered;
    }

    public Message(Profile sender, Profile receiver, String text, boolean delivered) {
        this(sender, receiver, text, (new Date(new java.util.Date().getTime())), (new Time(new java.util.Date().getTime())), delivered);
        /*this.sender = sender;
        this.receiver = receiver;
        this.text = secureText(text);
        this.dateMsg = new Date(new java.util.Date().getTime());
        this.timeMsg = new Time(new java.util.Date().getTime());
        this.delivered = false;*/
    } 
    
    public Message(Profile sender, Profile receiver, String text) {
        this(sender, receiver, text, (new Date(new java.util.Date().getTime())), (new Time(new java.util.Date().getTime())), false);
        /*this.sender = sender;
        this.receiver = receiver;
        this.text = secureText(text);
        this.dateMsg = new Date(new java.util.Date().getTime());
        this.timeMsg = new Time(new java.util.Date().getTime());
        this.delivered = false;*/
    }
    
    

    public Profile getSender() {
        return sender;
    }

    public Profile getReceiver() {
        return receiver;
    }

    public String getText() {
        return text;
    }

    public Date getDateMsg() {
        return dateMsg;
    }

    public Time getTimeMsg() {
        return timeMsg;
    }

    public boolean isDelivered() {
        return delivered;
    }

    public void setDelivered(boolean delivered) {
        this.delivered = delivered;
    }
    
    private String secureText(String text) {
        String safeText = new String();
        safeText = text.replaceAll("'", " ");
        return safeText;

    }

    @Override
    public String toString() {
        String stg = new String();
        stg = "From " + this.sender.getLogin() + "\n";
        stg = stg + "To " + this.receiver.getLogin() + "\n";
        stg = stg + "At " + this.dateMsg.toString() + " - " + this.timeMsg.toString() + "\n";
        stg = stg + this.text;
        return stg;
    }

}
